package com.wavehi.type_handler;

import com.wavehi.common.MyEnum;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Description
 * @Author WaveHi
 * @Date 2/12/2021 下午 2:09
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */
public class MyEnumTypeHandler implements TypeHandler<MyEnum> {


    /*
    指定参数设置时参数的值
     */
    @Override
    public void setParameter(
            PreparedStatement preparedStatement, int i,
            MyEnum myEnum, JdbcType jdbcType) throws SQLException {
        preparedStatement.setObject(i, myEnum.getCode());
    }

    /*
    设置数据库的值映射为Java类型的相应的数据
     */
    @Override
    public MyEnum getResult(ResultSet resultSet, String s) throws SQLException {
        int anInt = resultSet.getInt(s);
        MyEnum myEnum = MyEnum.forMyEnum(anInt);
        return myEnum;
    }

    /*
   设置数据库的值映射为Java类型的相应的数据
    */
    @Override
    public MyEnum getResult(ResultSet resultSet, int i) throws SQLException {
        int anInt = resultSet.getInt(i);
        MyEnum myEnum = MyEnum.forMyEnum(anInt);
        return myEnum;
    }

    /*
   设置数据库的值映射为Java类型的相应的数据
    */
    @Override
    public MyEnum getResult(CallableStatement callableStatement, int i)
            throws SQLException {
        int anInt = callableStatement.getInt(i);
        MyEnum myEnum = MyEnum.forMyEnum(anInt);
        return myEnum;
    }
}
